java Board的方块不会出现。
我有三门课:一门是主课:
public class Thera {
public static void main(String[] args) {
TheraGUI start = new TheraGUI();
}
}
一个是GUI
public class TheraGUI extends JFrame {
public TheraGUI(){
Board board = new Board();
this.setLayout(new GridBagLayout());
this.setTitle("Thera");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(board);
this.pack();
}
}
最后是我的董事会:
package thera;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Board extends JPanel {
JLayeredPane layeredpane;
JPanel board;
JLabel piece;
int xAdj;
int yAdj;
private static final String imageFolderPath = "src/resources/images/";
Dimension dimension = new Dimension(500, 500);
public Board(){
//create the layered pane
layeredpane = new JLayeredPane();
layeredpane.setPreferredSize(dimension);
this.add(layeredpane);
//create the Board
board = new JPanel();
board.setLayout(new GridLayout(7,9));
board.setPreferredSize(dimension);
layeredpane.add(board, JLayeredPane.DEFAULT_LAYER);
for(int c = 0; c < 7; c++){
for(int r = 0; r < 9; r++){
JPanel square = new JPanel();
square.setLayout(new BorderLayout());
square.setBackground(new Color(255, 204, 051));
board.add(square);
}
}
}
}
我的问题是:我的董事会不会出现! 附加信息:主布局是gridbaglayout,在第一单元中我添加了一个JPanel,在JPanel中我添加了一个layeredpane
# 1 楼答案
在设置可见性之后,您将添加一个组件(从而更改层次结构)。Swing在工作方式上有点迂腐